package com.ruoyi.fsepc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.fsepc.domain.StFseLocation;
import com.ruoyi.fsepc.domain.StFseLocationLog;
import com.ruoyi.fsepc.service.IStFseLocationLogService;
import com.ruoyi.fsepc.service.IStFseLocationService;
import com.ruoyi.system.api.ResponseWithData;
import com.ruoyi.system.api.domain.constant.ConfigKeys;
import com.ruoyi.system.api.service.IConfigurationService;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * FSE实时位置坐标Controller
 *
 * @author ruoyi
 * @date 2020-02-27
 */
@Controller
@RequestMapping("/fse/location")
public class StFseLocationController extends BaseController {
    private String prefix = "fsepc/location";

    @Autowired
    private IStFseLocationService stFseLocationService;
    @Autowired
    private IConfigurationService configurationService;
    @Autowired
    private IStFseLocationLogService stFseLocationLogService;

    /**
     * FSE实时监控
     *
     * @param
     * @return java.lang.String
     * @author wlf
     * @date 2020/2/5 15:28
     */
    @GetMapping("/realTime")
    public String realTime(ModelMap mmap) {
        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        mmap.put("roleId", roleId);
        mmap.put("tlId", StringUtils.isEmpty(user.getTeamleaderId()) ? "" : user.getTeamleaderId());
        mmap.put("fseId", null == user.getEnginnerId() ? "" : user.getEnginnerId());
        return prefix + "/realTime";
    }

    /**
     * 根据渣土车SIM卡号查询车辆状态集合
     */
    @PostMapping("/listByFseId")
    @ResponseBody
    public AjaxResult listByFseId(String fseId) {
        List<StFseLocation> list = new ArrayList<>();
        if (fseId != null && fseId.length() > 0) {
            list = stFseLocationService.selectListByFseId(fseId);
        }
        return AjaxResult.success("查询成功！", list);
    }

    /**
     * 查询team下fse位置信息
     *
     * @param fseLocation
     * @return
     */
    @PostMapping("/getFseLocation")
    @ResponseBody
    public AjaxResult getFseLocation(StFseLocation fseLocation) {
        if (!DateUtils.isWorkTime() || !DateUtils.isDatePeriod()) {
            return AjaxResult.success("不在工作时间！", new ArrayList<>());
        }
        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        List<StFseLocation> list = null;

        //经理 roleID=5； 经理登录查询所有FSE

        if (4 == roleId) {
            fseLocation.setTeamleaderId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
            fseLocation.setOnline("1");
            fseLocation.setWorkDay("1");
            list = stFseLocationService.getFseLocation(fseLocation);
        }
        if (1 == roleId || 2 == roleId || 5 == roleId) {
            fseLocation.setOnline("1");
            fseLocation.setWorkDay("1");
            list = stFseLocationService.getFseLocation(fseLocation);
        }
        if (3 == roleId) {
            fseLocation.setEnginnerId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
            fseLocation.setOnline("1");
            fseLocation.setWorkDay("1");
            list = stFseLocationService.getFseLocation(fseLocation);
        }

        return AjaxResult.success("查询成功！", list);
    }

    /**
     * 历史轨迹查询team下fse位置信息
     *
     * @param fseLocation
     * @return
     */
    @PostMapping("/getFseLocationHistory")
    @ResponseBody
    public AjaxResult getFseLocationHistory(StFseLocation fseLocation) {
        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        List<StFseLocation> list = new ArrayList();
//        if (!DateUtils.isWorkTime() || !DateUtils.isDatePeriod()) {
//            return AjaxResult.success("不在工作时间！", list);
//        }
        //经理 roleID=5； 经理登录查询所有FSE
        if (4 == roleId) {
            fseLocation.setTeamleaderId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
            list = stFseLocationService.getFseLocation(fseLocation);
        }
        if (1 == roleId || 2 == roleId || 5 == roleId) {
            list = stFseLocationService.getFseLocation(fseLocation);
        }
        if (3 == roleId) {
            fseLocation.setEnginnerId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
            //  fseLocation.setTeamleaderId(user.getEnginnerId().toString());
            list = stFseLocationService.getFseLocation(fseLocation);
        }
        return AjaxResult.success("查询成功！", list);
    }

    /**
     * 跳转列表
     *
     * @return
     */
    @RequiresPermissions("fse:location:view")
    @GetMapping()
    public String location() {
        return prefix + "/location";
    }

    /**
     * 查询FSE实时位置坐标列表
     */
    @RequiresPermissions("fse:location:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StFseLocation stFseLocation) {
        startPage();
        List<StFseLocation> list = stFseLocationService.selectStFseLocationList(stFseLocation);
        return getDataTable(list);
    }

    /**
     * 导出FSE实时位置坐标列表
     */
    /*@RequiresPermissions("fse:location:export")*/
    @Log(title = "FSE实时位置坐标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StFseLocation stFseLocation) {
        List<StFseLocation> list = stFseLocationService.selectStFseLocationList(stFseLocation);
        ExcelUtil<StFseLocation> util = new ExcelUtil<StFseLocation>(StFseLocation.class);
        return util.exportExcel(list, "location");
    }

    /**
     * 导出FSE实时位置
     *
     * @author qinheyang
     * @date 2020-04-30
     */
    @Log(title = "FSE实时位置坐标", businessType = BusinessType.EXPORT)
    @PostMapping("/exportByRole")
    @ResponseBody
    public AjaxResult exportByRole(StFseLocation stFseLocation) {

        SysUser user = ShiroUtils.getSysUser();
        Long roleId = user.getRoles().get(0).getRoleId();
        List<StFseLocation> list = null;
        List<StFseLocation> list1 = new ArrayList<>();
        //经理 roleID=5； 经理登录查询所有FSE
        if (DateUtils.isWorkTime() && DateUtils.isDatePeriod()) {
            if (4 == roleId) {
                stFseLocation.setTeamleaderId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
                stFseLocation.setWorkDay("1");
                stFseLocation.setOnline("1");
                list = stFseLocationService.getFseLocation(stFseLocation);
            }
            if (1 == roleId || 2 == roleId || 5 == roleId) {
                stFseLocation.setWorkDay("1");
                stFseLocation.setOnline("1");
                list = stFseLocationService.getFseLocation(stFseLocation);
            }
            if (3 == roleId) {
                stFseLocation.setWorkDay("1");
                stFseLocation.setOnline("1");
                stFseLocation.setEnginnerId(null == user.getEnginnerId() ? null : user.getEnginnerId() + "");
                //  fseLocation.setTeamleaderId(user.getEnginnerId().toString());
                list = stFseLocationService.getFseLocation(stFseLocation);
            }
//        SysUser user = ShiroUtils.getSysUser();
//        Long roleId = user.getRoles().get(0).getRoleId();
//
//        List<StFseLocation> list =new ArrayList<>();
//
//        stFseLocation.setTeamleaderId( String.valueOf(user.getEnginnerId()));
//        stFseLocation.setWorkDay("1");
//
//        //经理 roleID=5； 经理登录查询所有FSE
//
//        if (5 == roleId || 4 == roleId) {
//            stFseLocation.setTeamleaderId(null==user.getEnginnerId()?null:user.getEnginnerId()+"");
//            stFseLocation.setWorkDay("1");
//            list = stFseLocationService.getFseLocation(stFseLocation);
//        }
//        if(1==roleId || 2==roleId){
//            list = stFseLocationService.getFseLocation(stFseLocation);
//        }
//        if(3==roleId) {
//            stFseLocation.setEnginnerId( null==user.getEnginnerId()?null:user.getEnginnerId()+"");
//            list = stFseLocationService.getFseLocation(stFseLocation);
//        }
            for (StFseLocation stFseLocation1 : list) {
                if ((stFseLocation1.getPostx() == null || stFseLocation1.getPosty() == null) && roleId != 3) {
                    continue;
                }
                if (stFseLocation1.getState() != null) {
                    if (stFseLocation1.getState().equals("1")) {
                        stFseLocation1.setState("可用");
                    } else if (stFseLocation1.getState().equals("0")) {
                        stFseLocation1.setState("不可用");
                    }
                }
                list1.add(stFseLocation1);
            }
        }
        ExcelUtil<StFseLocation> util = new ExcelUtil<StFseLocation>(StFseLocation.class);
        return util.exportExcel(list1, "location");
    }

    /**
     * 新增FSE实时位置坐标
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存FSE实时位置坐标
     */
    @RequiresPermissions("fse:location:add")
    @Log(title = "FSE实时位置坐标", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StFseLocation stFseLocation) {
        return toAjax(stFseLocationService.insertStFseLocation(stFseLocation));
    }

    /**
     * 修改FSE实时位置坐标
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        StFseLocation stFseLocation = stFseLocationService.selectStFseLocationById(id);
        mmap.put("stFseLocation", stFseLocation);
        return prefix + "/edit";
    }

    /**
     * 修改保存FSE实时位置坐标
     */
    @RequiresPermissions("fse:location:edit")
    @Log(title = "FSE实时位置坐标", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StFseLocation stFseLocation) {
        return toAjax(stFseLocationService.updateStFseLocation(stFseLocation));
    }

    /**
     * 删除FSE实时位置坐标
     */
    @RequiresPermissions("fse:location:remove")
    @Log(title = "FSE实时位置坐标", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(stFseLocationService.deleteStFseLocationByIds(ids));
    }
}
